import requests
import execjs


class Spider:
    def __init__(self):
        self.headers = {
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
        }

    def compile_js(self, function_name, **kwargs):
        params = kwargs['params']
        with open('b站.js', 'r', encoding='utf-8') as f:
            js_code = f.read()
        result = execjs.compile(js_code).call(function_name, params)
        return result

    def start_request(self):
        url = "https://api.bilibili.com/x/v2/reply/wbi/main"
        params = {
            "oid": "1506185539",
            "type": "1",
            "mode": "3",
            "pagination_str": "{\"offset\":\"\"}",
            "plat": "1",
            "seek_rpid": "",
            "web_location": "1315875",
            # "w_rid": "a4ffa51623dfcc7d973fd88ed7a82793",
            # "wts": "1717260946"
        }
        w_rid_wts = self.compile_js("get_w_rid", params=params)
        params.update(w_rid_wts)
        response = requests.get(url, headers=self.headers, params=params)
        print(response.text)


if __name__ == '__main__':
    Spider().start_request()
